home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 297 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: prefix vs. postfix
  5. Date: 3 Jan 1996 20:49 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <3JAN199620494722@erich.triumf.ca>
  9. References: <4c1089$gro@nntpd2.cxo.dec.com> <tcpnntpd.15.12.30.11.27.18.2781597121.305087@the-fix.sos.on.ca> <4cf35v$7il@head.globalcom.net>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4cf35v$7il@head.globalcom.net>, scicom@globalcom.net (stephen j beaver) writes...
  14. ><SNIP>
  15. >>The two operators _never_ do the same thing.  However, if you just want the
  16. >>side effect of the increment, but the value returned by i++ or ++i is not used,
  17. >>(as in for(i = 0; i < 10; i++) ) it doesn't matter which is used.
  18. >I thought that:
  19. >for(i=0;i<10;i++)
  20. >   {
  21. >   loopbody(i):
  22. >   }
  23. >Increments i after the loop so that the first value passed to loopbody() is zero, whereas:
  24. >for(i=0;i<10;++i)
  25. >   {
  26. >  loopbody();
  27. >   }
  28. >increments i before the loop body so that the first value passedtp loopbody() is 1 in this case.
  29. >No?
  30.  
  31. NO!
  32.  
  33. The "i++" (or "++i") can be thought of as occuring after the body of the loop,
  34. just before the program jumps back to the top of the loop.
  35.  
  36. Writing this loop:
  37.     for(i = 0; i < 10; i++)
  38.                 {
  39.         loopbody(i);
  40.                 }
  41. with gotos gives something like this:
  42.     i = 0;
  43.      loop:
  44.     if(!(i < 10)) goto end
  45.     loopbody(i);
  46.     i++;
  47.     goto loop;
  48.      end:
  49.     carry on...
  50. so whether you use i++ or ++i is irrelevant - the first time through the loop,
  51. i will be 0.
  52.  
  53. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  54. Internet: bennett@triumf.ca         | of one another only when one can be
  55. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  56. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  57. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.